package com.xls.future.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.method.KeyListener;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.xls.future.R;
import com.xls.future.entity.Feedback;
import com.xls.future.utils.AppUtil;
import com.xls.future.utils.LogUtil;
import com.xls.future.utils.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

/**
 * Created by Administrator on 2017/8/22.
 */

public class MineFragment extends BaseFragment {

    private static String TAG = "MineFragment";
    Unbinder unbinder;
    @BindView(R.id.tv_channel)
    TextView tvChannel;
    @BindView(R.id.tv_version)
    TextView tvVersion;
    @BindView(R.id.et_feedback)
    EditText etFeedback;
    @BindView(R.id.btn_commit_feedback)
    Button btnCommitFeedback;


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        Log.d("xls", "MineFragment onAttach");
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("xls", "MineFragment onCreate");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.d("xls", "MineFragment onCreateView");
        View view = inflater.inflate(R.layout.fragment_mine, container, false);
        unbinder = ButterKnife.bind(this, view);
        initViews();
        return view;
    }

    private void initViews() {
        tvVersion.setText(AppUtil.getVersionName(getContext().getApplicationContext()));
        tvChannel.setText(AppUtil.getChannel(getContext().getApplicationContext()));
        etFeedback.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if(keyCode==KeyEvent.KEYCODE_ENTER && event.getAction()==KeyEvent.ACTION_UP){
                    commitFeedback();
                    return true;
                }
                return false;
            }
        });
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.d("xls", "MineFragment onActivityCreated");
    }

    @Override
    public void onStart() {
        Log.d("xls", "MineFragment onStart");
        super.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d("xls", "MineFragment onResume");
        if(isFirstVisible){
            LogUtil.d("xiong","MineFragment 第一次可见");

        }
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.d("xls", "MineFragment onPause");
    }

    @Override
    public void onStop() {
        super.onStop();
        Log.d("xls", "MineFragment onStop");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.d("xls", "MineFragment onDestroyView");
        unbinder.unbind();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("xls", "MineFragment onDestroy");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        Log.d("xls", "MineFragment onDetach");
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        Log.d("xls", "MineFragment onHiddenChanged : " + hidden);
        if (hidden) {
            //隐藏
            isFirstVisible = false;
        } else {
            //显示
            LogUtil.d("xiong","MineFragment 显示");
        }
    }



    @OnClick(R.id.btn_commit_feedback)
    public void onViewClicked() {
        commitFeedback();
    }


    private void commitFeedback(){
        String content = etFeedback.getText().toString().trim();
        if(TextUtils.isEmpty(content)){
            ToastUtil.show(getContext(),"亲,请输入内容再点发表哦");
            return;
        }
        String deviceId = AppUtil.getDeviceId(getContext());
        String deviceType = AppUtil.getDeviceType();
        String appVersion = AppUtil.getVersionName(getContext());
        String appChannel = AppUtil.getChannel(getContext());

        Feedback feedback = new Feedback();
        feedback.setDeviceId(deviceId);
        feedback.setDeviceType(deviceType);
        feedback.setAppVersion(appVersion);
        feedback.setAppChannel(appChannel);
        feedback.setContent(content);
        feedback.save(new SaveListener<String>() {
            @Override
            public void done(String objectId, BmobException e) {
                LogUtil.d(TAG,"objectId ="+objectId+" | e ="+e);
                if(e==null){
                    ToastUtil.show(getContext(),"发表成功！");
                    etFeedback.setText("");
                }else{
                    ToastUtil.show(getContext(),"发表失败！"+e.getMessage());
                }
            }
        });
    }
}
